/************************************************************************************/
/*  Program name CAPGLIMP_INTERFACE.sql                                        	    */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 7 FEB, 2006   NIP Karen         First created                                    */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_INTERFACE';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_INTERFACE;
	
	lv_COAID		NUMBER;
	R22				VARCHAR2(240);
	R23				VARCHAR2(240);
	R25				VARCHAR2(240);
	R27				VARCHAR2(240);
	R29				VARCHAR2(240);
	
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			lv_COAID	:=	CAPBRIMP_COMMON.GetCOAIDfromFlexStrCode(C1.FLEX_STR_CODE);
			
			R22	:=	C1.REFERENCE22;
			R23	:=	C1.REFERENCE23;
			R25	:=	C1.REFERENCE25;
			R27	:=	C1.REFERENCE27;
			R29	:=	C1.REFERENCE29;
			
			IF		C1.USER_JE_CATEGORY_NAME	=	'Purchase Invoices'	
			AND		C1.USER_JE_SOURCE_NAME		=	'Payables'			THEN
				R22	:=	TO_CHAR(CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(TO_NUMBER(C1.REFERENCE22)));
				R27	:=	TO_CHAR(CAPBRIMP_MAPPING.GetSOBIDMapping(TO_NUMBER(C1.REFERENCE27)));

			ELSIF	C1.USER_JE_CATEGORY_NAME	=	'Payments'	
			AND		C1.USER_JE_SOURCE_NAME		=	'Payables'			THEN
				IF	R22	IS NOT NULL	THEN
					R22	:=	TO_CHAR(CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(TO_NUMBER(C1.REFERENCE22)));
				END IF;
				R23	:=	TO_CHAR(CAPBRIMP_MAPPING.GetCheckIDMapping(TO_NUMBER(C1.REFERENCE23)));
				R27	:=	TO_CHAR(CAPBRIMP_MAPPING.GetSOBIDMapping(TO_NUMBER(C1.REFERENCE27)));
				IF	R29	IS NOT NULL	THEN
					R29	:=	TO_CHAR(CAPBRIMP_MAPPING.GetInvPaymentIDMapping(TO_NUMBER(C1.REFERENCE29)));
				END IF;

			ELSIF	C1.USER_JE_CATEGORY_NAME	=	'Sales Invoices'	
			AND		C1.USER_JE_SOURCE_NAME		=	'Receivables'			THEN
				R22	:=	TO_CHAR(CAPBRIMP_MAPPING.GetRACustTRXIDMapping(TO_NUMBER(C1.REFERENCE22)));
				R23	:=	TO_CHAR(CAPBRIMP_MAPPING.GetRACustTRXLnDistIDMapping(TO_NUMBER(C1.REFERENCE23)));
				R27	:=	TO_CHAR(CAPBRIMP_MAPPING.GetRACustomerIDMapping(TO_NUMBER(C1.REFERENCE27)));

			ELSIF	C1.USER_JE_CATEGORY_NAME	=	'Trade Receipts'	
			AND		C1.USER_JE_SOURCE_NAME		=	'Receivables'			THEN
				IF	C1.REFERENCE29	=	'TRADE_CASH'	THEN	
					R22	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARCashReceiptIDMapping(TO_NUMBER(SUBSTR(C1.REFERENCE22,1,4))))
							||'C'||
							TO_CHAR(CAPBRIMP_MAPPING.GetARCashReceiptHistIDMapping(TO_NUMBER(SUBSTR(C1.REFERENCE22,-4))));
				ELSE
					R22	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARCashReceiptIDMapping(TO_NUMBER(SUBSTR(C1.REFERENCE22,1,4))))
							||'C'||
							TO_CHAR(CAPBRIMP_MAPPING.GetARReceivableAppIDMapping(TO_NUMBER(SUBSTR(C1.REFERENCE22,-4))));
				END IF;
				R23	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARLineIDMapping(TO_NUMBER(C1.REFERENCE23)));
				R27	:=	TO_CHAR(CAPBRIMP_MAPPING.GetRACustomerIDMapping(TO_NUMBER(C1.REFERENCE27)));

			ELSIF	C1.USER_JE_CATEGORY_NAME	=	'Misc Receipts'	
			AND		C1.USER_JE_SOURCE_NAME		=	'Receivables'			THEN
				R22	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARCashReceiptIDMapping(TO_NUMBER(C1.REFERENCE22)));
				R23	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARLineIDMapping(TO_NUMBER(C1.REFERENCE23)));
				IF	C1.REFERENCE29	=	'MISC_CASH'	THEN	
					R25	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARCashReceiptHistIDMapping(TO_NUMBER(C1.REFERENCE25)));
				ELSE
					R25	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARMiscCashDistIDMapping(TO_NUMBER(C1.REFERENCE25)));
				END IF;
			
			ELSIF	(	C1.USER_JE_CATEGORY_NAME	=	'Purchases'
					OR	C1.USER_JE_CATEGORY_NAME	=	'Accrual'
					OR	C1.USER_JE_CATEGORY_NAME	=	'Requisitions')
			AND		C1.USER_JE_SOURCE_NAME		=	'Purchasing'	THEN
				--IF	REFERENCE21	=	'PO'	THEN
				IF	C1.REFERENCE21	=	'PO'	THEN	
					R22	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPOHeaderIDMapping(TO_NUMBER(C1.REFERENCE22)));
				--ELSIF	REFERENCE21	=	'REQ'	THEN
				ELSIF	C1.REFERENCE21	=	'REQ'	THEN
					R22	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPOReqHeaderIDMapping(TO_NUMBER(C1.REFERENCE22)));
				END IF;
				--IF	REFERENCE21	=	'PO'	THEN
				IF	C1.REFERENCE21	=	'PO'	THEN
					R23	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPODistIDMapping(TO_NUMBER(C1.REFERENCE23)));
				--ELSIF	REFERENCE21	=	'REQ'	THEN
				ELSIF	C1.REFERENCE21	=	'REQ'	THEN
					R23	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPOReqDistIDMapping(TO_NUMBER(C1.REFERENCE23)));
				END IF;
			
			ELSIF	C1.USER_JE_CATEGORY_NAME	=	'Receiving'
			AND		C1.USER_JE_SOURCE_NAME		=	'Purchasing'	THEN
				R22	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPOHeaderIDMapping(TO_NUMBER(C1.REFERENCE22)));
				R23	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPODistIDMapping(TO_NUMBER(C1.REFERENCE23)));
				R25	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPOTXNIDMapping(TO_NUMBER(C1.REFERENCE25)));
			END IF;
			
			INSERT INTO GL_INTERFACE
			VALUES
			(
			C1.STATUS,
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),
			C1.ACCOUNTING_DATE,
			C1.CURRENCY_CODE,
			C1.DATE_CREATED,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.ACTUAL_FLAG,
			C1.USER_JE_CATEGORY_NAME,
			C1.USER_JE_SOURCE_NAME,
			C1.CURRENCY_CONVERSION_DATE,
			CAPBRIMP_COMMON.GetEncumTypeID(C1.ENCUM_TYPE),
			CAPBRIMP_COMMON.GetBudgetVerID(C1.BUDGET_NAME),
			C1.USER_CURRENCY_CONVERSION_TYPE,
			C1.CURRENCY_CONVERSION_RATE,
			C1.AVERAGE_JOURNAL_FLAG,
			C1.ORIGINATING_BAL_SEG_VALUE,
			C1.SEGMENT1,
			C1.SEGMENT2,
			C1.SEGMENT3,
			C1.SEGMENT4,
			C1.SEGMENT5,
			C1.SEGMENT6,
			C1.SEGMENT7,
			C1.SEGMENT8,
			C1.SEGMENT9,
			C1.SEGMENT10,
			C1.SEGMENT11,
			C1.SEGMENT12,
			C1.SEGMENT13,
			C1.SEGMENT14,
			C1.SEGMENT15,
			C1.SEGMENT16,
			C1.SEGMENT17,
			C1.SEGMENT18,
			C1.SEGMENT19,
			C1.SEGMENT20,
			C1.SEGMENT21,
			C1.SEGMENT22,
			C1.SEGMENT23,
			C1.SEGMENT24,
			C1.SEGMENT25,
			C1.SEGMENT26,
			C1.SEGMENT27,
			C1.SEGMENT28,
			C1.SEGMENT29,
			C1.SEGMENT30,
			C1.ENTERED_DR,
			C1.ENTERED_CR,
			C1.ACCOUNTED_DR,
			C1.ACCOUNTED_CR,
			C1.TRANSACTION_DATE,
			C1.REFERENCE1,
			C1.REFERENCE2,
			C1.REFERENCE3,
			C1.REFERENCE4,
			C1.REFERENCE5,
			C1.REFERENCE6,
			C1.REFERENCE7,
			C1.REFERENCE8,
			C1.REFERENCE9,
			C1.REFERENCE10,
			C1.REFERENCE11,
			C1.REFERENCE12,
			C1.REFERENCE13,
			C1.REFERENCE14,
			C1.REFERENCE15,
			C1.REFERENCE16,
			C1.REFERENCE17,
			C1.REFERENCE18,
			C1.REFERENCE19,
			C1.REFERENCE20,
			C1.REFERENCE21,
			R22,
			R23,
			C1.REFERENCE24,
			R25,
			C1.REFERENCE26,
			R27,
			C1.REFERENCE28,
			R29,
			C1.REFERENCE30,
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.JE_BATCH_ID),
			C1.PERIOD_NAME,
			CAPBRIMP_MAPPING.GetJEHeaderIDMapping(C1.JE_HEADER_ID),
			C1.JE_LINE_NUM,
			lv_COAID,
			C1.FUNCTIONAL_CURRENCY_CODE,
			CAPBRIMP_COMMON.GetSegment(lv_COAID,C1.CCID_D),
			C1.DATE_CREATED_IN_GL,
			C1.WARNING_CODE,
			C1.STATUS_DESCRIPTION,
			C1.STAT_AMOUNT,
			C1.GROUP_ID,
			C1.REQUEST_ID,
			CAPBRIMP_COMMON.GetDocSeqID(C1.DOC_SEQ_NAME),
			C1.SUBLEDGER_DOC_SEQUENCE_VALUE,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.GL_SL_LINK_ID,
			C1.GL_SL_LINK_TABLE,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE16,
			C1.ATTRIBUTE17,
			C1.ATTRIBUTE18,
			C1.ATTRIBUTE19,
			C1.ATTRIBUTE20,
			C1.CONTEXT,
			C1.CONTEXT2,
			C1.INVOICE_DATE,
			C1.TAX_CODE,
			C1.INVOICE_IDENTIFIER,
			C1.INVOICE_AMOUNT,
			C1.CONTEXT3,
			C1.USSGL_TRANSACTION_CODE,
			C1.DESCR_FLEX_ERROR_MESSAGE,
			C1.JGZZ_RECON_REF,
			C1.REFERENCE_DATE
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/